package cn.chinaflame.pagodas.web;

import cn.chinaflame.pagodas.Result.Requests;
import cn.chinaflame.pagodas.Result.Result;
import cn.chinaflame.pagodas.utils.Jackson;
import org.springframework.util.StreamUtils;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;


public class OpenAPI {

    private static final String[] IGNORE_FIELDS = new String[]{"ok", "error", "owner"};

    public static String writeJson(Result<?> result) {
        return Jackson.writeJson(result, IGNORE_FIELDS);
    }

    public static String entityString() {
        try (InputStream in = Requests.getRequest().getInputStream()) {
            return StreamUtils.copyToString(in, Charset.forName("UTF-8"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
